package cn.itcast.controller;

import cn.itcast.entity.*;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.SetmealService;
import cn.itcast.utils.FileNameUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.qiniu.util.Json;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.BinaryClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/setmeal")
public class SetmealController {


    @Reference
    SetmealService service;

    @Autowired
    JedisPool jedisPool;


    @RequestMapping("/upload.do")
    public Result upload(MultipartFile imgFile) throws IOException {

        //上传去哪
        String path = "http://localhost:8080/fileServer/upload/";
        //获取上传文件名称
        String oldName = imgFile.getOriginalFilename();
        //改文件名
        String newName = FileNameUtils.getName(oldName);


        try {
            //创建客端
            Client client = Client.create();
            //设置上传路径与文件名
            WebResource resource = client.resource(path + newName);
            //上传！（其本质文件以字节流的形式重新写入到其它服务器上）
            resource.put(imgFile.getBytes());

            Jedis jedis = jedisPool.getResource();
            //将图片名称保存到redis服务器中
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newName);

        }catch(Exception e){
            e.printStackTrace();

            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

        //用于图片在页面端进行回显
        return new Result(false, MessageConstant.PIC_UPLOAD_SUCCESS,newName);
    }






    @RequestMapping("/add.do")
    public Result add(@RequestBody Object[] obj) {
        //crud 上传 下载  excel  报表


        //将json数据转换成javaBean
        Setmeal sm = JSON.parseObject(obj[1].toString(),Setmeal.class);
        //将json数据转换成集合
        List<Integer> ids = JSON.parseArray(obj[0].toString(), Integer.class);


        try {

            service.add(ids, sm);

            Jedis jedis = jedisPool.getResource();
            //将图片名称保存到redis服务器中
            jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,sm.getImg());


        }catch(Exception e){
            e.printStackTrace();

            return new Result(true,MessageConstant.ADD_SETMEAL_FAIL);
        }

       return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);

    }


    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean bean) {

        return service.findPage(bean);

    }

    @RequestMapping("/findAll")
    public List<Setmeal> findAll(){

        return service.findAll();
    }

}
